package com.hardware.view.controller;

import com.hardware.entity.CardInfo;
import com.hardware.entity.DoorInit;
import com.hardware.entity.Respond;
import com.hardware.enums.*;
import com.hardware.view.service.FunctionService;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.stage.Stage;
import org.yaml.snakeyaml.Yaml;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

/**
 * 功能测试
 *
 * @作者 韩宗森
 * @创建时间 2024年11月05日 11:52
 */
public class FunctionController {

    @FXML
    private CheckBox isCover;

    @FXML
    private TextField serverText;

    @FXML
    private ChoiceBox<TmEncoderEnum> tmEncoderChoice;

    @FXML
    private ChoiceBox<DoorLockEnum> doorSoftwareChoice;

    @FXML
    private ChoiceBox<PortEnum> portChoice;

    @FXML
    private ChoiceBox<EncoderEnum> encoderChoice;

    @FXML
    private ChoiceBox<BeepEnum> beepChoice;

    @FXML
    private TextField publicWayText;

    @FXML
    private TextField usernameText;

    @FXML
    private TextField roomNum;

    @FXML
    private TextField endTime;

    @FXML
    private TextField starTime;

    @FXML
    private TextField guestName;

    @FXML
    private TextField guestId;

    @FXML
    private TextField cardNum;

    @FXML
    private TextField roomNumRead;

    @FXML
    private TextField endTimeRead;

    @FXML
    private TextField starTimeRead;

    @FXML
    private TextField guestNameRead;

    @FXML
    private TextField guestIdRead;

    @FXML
    private TextField cardStatus;

    @FXML
    private TextField cardNumRead;


    private FunctionService functionService;

    /**
     * 窗口初始化函数
     */
    @FXML
    public void initialize() {
        // 读取 yml 的值 爱迪尔门锁初始化默认数据
       // DoorInit ymlData = getYmlData();

        // 创建 functionService 对象
        functionService = new FunctionService();


        // 初始化 门锁软件 下拉框选项
        functionService.initializeChoiceBox(doorSoftwareChoice, DoorLockEnum.class,18);
        doorSoftwareChoice.setOnAction(event -> doorSoftwareChoiceHandle()); // 绑定事件

        // 初始化 端口 下拉框选项
        functionService.initializeChoiceBox(portChoice, PortEnum.class,1);
        portChoice.setOnAction(event -> portChoiceHandle());

        // 初始化 发行机类型 下拉框选项
        functionService.initializeChoiceBox(encoderChoice, EncoderEnum.class,0);
        encoderChoice.setOnAction(event -> issueTypeChoiceHandle());

        // 初始化 TM发行机类型 下拉框选项
        functionService.initializeChoiceBox(tmEncoderChoice, TmEncoderEnum.class,1);
        tmEncoderChoice.setOnAction(event -> tmIssueTypeChoiceHandle());

        // 初始化 beep 蜂鸣器 下拉框选项
        functionService.initializeChoiceBox(beepChoice, BeepEnum.class,11);
        beepChoice.setOnAction(event -> beepChoiceHandle());



        serverText.setText("10.147.18.167"); // 服务器
        usernameText.setText("admin"); // 用户名

    }


    /**
     * 门锁软件 下拉框事件
     */
    private void doorSoftwareChoiceHandle() {
        DoorLockEnum selectedItem = doorSoftwareChoice.getValue();
        System.out.println(" Key: " + selectedItem.getKey() + " Value: " + selectedItem.getValue());
    }

    /**
     * 端口 下拉框事件
     */
    private void portChoiceHandle() {
        PortEnum selectedItem = portChoice.getValue();
        System.out.println(" Key: " + selectedItem.getKey() + " Value: " + selectedItem.getValue());
    }

    /**
     * 发行机类型 下拉框事件
     */
    private void issueTypeChoiceHandle() {
        EncoderEnum selectedItem = encoderChoice.getValue();
        System.out.println(" Key: " + selectedItem.getKey() + " Value: " + selectedItem.getValue());
    }

    /**
     * TM发行机类型 下拉框事件
     */
    private void tmIssueTypeChoiceHandle() {
        TmEncoderEnum selectedItem = tmEncoderChoice.getValue();
        System.out.println(" Key: " + selectedItem.getKey() + " Value: " + selectedItem.getValue());
    }

    /**
     * 蜂鸣器 下拉框事件
     */
    private void beepChoiceHandle() {
        BeepEnum selectedItem = beepChoice.getValue();
        System.out.println(" Key: " + selectedItem.getKey() + " Value: " + selectedItem.getValue());
    }

    /**
     * 设置串口
     *
     * @param event
     */
    @FXML
    void setPortAction(ActionEvent event) {
        Integer doorSoftwareKey = doorSoftwareChoice.getValue().getKey(); // 门锁软件
        Integer portKey = portChoice.getValue().getKey(); // 端口
        Integer encoderKey = encoderChoice.getValue().getKey();// 发行机类型
        Integer tmEncoderKey = tmEncoderChoice.getValue().getKey(); // TM发行机类型

        System.out.println("设置串口 ...");
        int ret = functionService.setPort(doorSoftwareKey, portKey, encoderKey, tmEncoderKey);
        showRespond(ret);
    }

    /**
     * Beep(蜂鸣器)
     *
     * @param event
     */
    @FXML
    void beep(ActionEvent event) {
        System.out.println("Beep(蜂鸣器) ...");
        Integer key = beepChoice.getValue().getKey();
        int ret = functionService.beep(key);
        showRespond(ret);
    }

    /**
     * 初始化
     *
     * @param event
     */
    @FXML
    void doorInit(ActionEvent event) {
        String serverStr = serverText.getText(); // 服务器框
        Integer softwareKey = doorSoftwareChoice.getValue().getKey(); // 门锁软件
        Integer portKey = portChoice.getValue().getKey(); // 端口
        Integer encoderKey = encoderChoice.getValue().getKey();// 发行机类型
        Integer tmEncoderKey = tmEncoderChoice.getValue().getKey(); // TM发行机类型
        String usernameStr = usernameText.getText();  // 用户名

        int ret = functionService.doorInit(softwareKey, serverStr, usernameStr, portKey, encoderKey, tmEncoderKey);
        showRespond(ret);
    }

    /**
     * 结束 工作期
     *
     * @param event
     */
    @FXML
    void close(ActionEvent event) {
        System.out.println("结束 ...");
        int ret = functionService.close();
        showRespond(ret);
    }

    /**
     * 取消 按钮事件
     *
     * @param event
     */
    @FXML
    void cancel(ActionEvent event) {
        // 获取当前窗口的 Stage 对象
        Stage stage = (Stage) ((Button) event.getSource()).getScene().getWindow();
        // 关闭窗口
        stage.close();
    }


    /**
     * 发卡
     *
     * @param event
     */
    @FXML
    void writeCard(ActionEvent event) {
        String roomNumText = roomNum.getText(); // 房号
        String publicWayStr = publicWayText.getText(); // 公共通道
        String starTimeText = starTime.getText(); // 开始时间
        String endTimeText = endTime.getText(); // 结束时间
        String cardNumText = cardNum.getText(); // 卡号
        String guestNameText = guestName.getText(); // 客人姓名
        String guestIdText = guestId.getText(); // 客人id
        boolean isCoverText = isCover.isSelected(); // 是否覆盖


        int ret = functionService.writeCard(roomNumText, publicWayStr, starTimeText + endTimeText,
                guestNameText, guestIdText, isCoverText ? 1 : 0, cardNumText);
        showRespond(ret);
    }

    /**
     * 复制卡
     *
     * @param event
     */
    @FXML
    void copyCard(ActionEvent event) {
        String roomNumText = roomNum.getText(); // 房号
        String publicWayStr = publicWayText.getText(); // 公共通道
        String starTimeText = starTime.getText(); // 开始时间
        String endTimeText = endTime.getText(); // 结束时间
        String cardNumText = cardNum.getText(); // 卡号
        String guestNameText = guestName.getText(); // 客人姓名
        String guestIdText = guestId.getText(); // 客人id
        boolean isCoverText = isCover.isSelected(); // 是否覆盖


        int ret = functionService.copyCard(roomNumText, publicWayStr, starTimeText + endTimeText,
                guestNameText, guestIdText, isCoverText ? 1 : 0, cardNumText);
        showRespond(ret);
    }

    /**
     * 读卡
     * @param event
     */
    @FXML
    void readCard(ActionEvent event) {
        Respond respond = functionService.readCard();
        CardInfo cardInfo = (CardInfo)respond.getData();

        roomNumRead.setText(cardInfo.getRoom()); // 房号
        publicWayText.setText(cardInfo.getGate()); // 公共通道
        starTimeRead.setText(cardInfo.getStarTime()); // 开始时间
        endTimeRead.setText(cardInfo.getEndTime()); // 结束时间
        guestNameRead.setText(cardInfo.getGuestName()); // 客人姓名
        guestIdRead.setText(cardInfo.getGuestId()); // 客人id
        cardNumRead.setText(String.valueOf(cardInfo.getCardno())); // 卡号
        cardStatus.setText(String.valueOf(cardInfo.getCardStatus())); // 状态

        showRespond(respond.getStatus());
    }

    /**
     * 销卡
     * @param event
     */
    @FXML
    void destroyCard(ActionEvent event) {

    }

    /**
     * 显示响应信息
     *
     * @param ret
     */
    private void showRespond(int ret) {
        AidierStatus aidierStatus = AidierStatus.fromKey(ret);
        // 创建一个 Alert 对象
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle("响应信息提示");
        alert.setHeaderText(null); // 不给 null 会有默认信息
        alert.setContentText("状态码: " + ret + "; 说明: " + aidierStatus.getValue());
        // 显示提示框
        alert.showAndWait();
    }

    /**
     * 获取 yml 的属性
     */
    private DoorInit getYmlData() {
        // 定义文件路径
        String filePath = "src/main/resources/application.yml";

        DoorInit doorInit = new DoorInit();

        Yaml yaml = new Yaml();
        try (InputStream in = new FileInputStream(filePath)) {
            // 将 YAML 文件内容转换为 Map
            Map<String, Object> data = yaml.load(in);

            // 读取属性
            Map<String, Object> appData = (Map<String, Object>) data.get("aidier");
            if (appData != null) {
                doorInit.setSoftware((Integer) appData.get("software"));
                doorInit.setServer((String) appData.get("server"));
                doorInit.setUsername((String) appData.get("username"));
                doorInit.setPort((Integer) appData.get("port"));
                doorInit.setEncoder((Integer) appData.get("encoder"));
                doorInit.setTmEncoder((Integer) appData.get("tmEncoder"));
                System.out.println("yml读到的参数doorInit : " + doorInit);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return doorInit;
    }


}
